﻿var free_areas = [];
var province_count = 0;
var area_group_count_index = 1;
var area_group_count = 1;
var all_areas = [];
$(function () {
    $('.ul_province li')['each'](function () {
        var _0xa877x6 = {};
        _0xa877x6['province_code'] = $(this)['attr']('id');
        _0xa877x6['province'] = $(this)['text']();
        all_areas['push'](_0xa877x6)
    });
    $('#by_piece')['attr']('checked', 'checked');
    $('[name=\'chargetype\']')['each'](function () {
        $(this)['click'](function () {
            var _0xa877x7 = $(this)['val']();
            if (typeof($(this)['attr']('checked')) == 'undefined') {
                $(this)['attr']('checked', 'checked');
                if (_0xa877x7 == 3) {
                    $('#by_piece')['removeAttr']('checked')
                } else {
                    $('#by_weight')['removeAttr']('checked')
                };
                var _0xa877x8 = _0xa877x7 == 2 ? '件' : '重';
                var _0xa877x9 = _0xa877x7 == 2 ? '件' : 'g';
                $('.distribution_area')['each'](function () {
                    $(this)['find']('.fee_tip01')['each'](function (_0xa877xa, _0xa877xb) {
                        $(_0xa877xb)['text']('首' + _0xa877x8 + '：')
                    });
                    $(this)['find']('.fee_tip02')['each'](function (_0xa877xa, _0xa877xb) {
                        $(_0xa877xb)['html']('&nbsp;' + _0xa877x9)
                    });
                    $(this)['find']('.fee_tip11')['each'](function (_0xa877xa, _0xa877xb) {
                        $(_0xa877xb)['text']('续' + _0xa877x8 + '：')
                    })
                })
            }
        })
    });
    $('.ul_province li')['each'](function () {
        $(this)['click'](function () {
            var _0xa877x6 = {};
            _0xa877x6['province_code'] = $(this)['attr']('id');
            _0xa877x6['province'] = $(this)['text']();
            if (typeof($(this)['attr']('style')) == 'undefined') {
                var _0xa877xc = IndexInArray(all_areas, _0xa877x6);
                if (_0xa877xc > -1) {
                    all_areas['splice'](_0xa877xc, 1);
                    $(this)['attr']('style', 'border:1px red solid;');
                    free_areas['push'](_0xa877x6);
                    province_count++
                } else {
                    layer['msg']('\u8BE5\u533A\u57DF\u5DF2\u8BBE\u7F6E', {
                        icon: 5,
                        time: 1200
                    });
                    return false
                }
            } else {
                var _0xa877xd = IndexInArray(free_areas, _0xa877x6);
                if (_0xa877xd > -1) {
                    $(this)['removeAttr']('style');
                    free_areas['splice'](_0xa877xd, 1);
                    province_count--;
                    all_areas['push'](_0xa877x6)
                }
            };
            $('#provinceCount')['text'](province_count);
            console['log'](_0xa877x6);
            console['log'](free_areas)
        })
    });
    $('#select_all')['click'](function () {
        $('.ul_province li')['each'](function () {
            if (typeof($(this)['attr']('style')) == 'undefined') {
                var _0xa877x6 = {};
                _0xa877x6['province_code'] = $(this)['attr']('id');
                _0xa877x6['province'] = $(this)['text']();
                var _0xa877xc = IndexInArray(all_areas, _0xa877x6);
                if (_0xa877xc > -1) {
                    all_areas['splice'](_0xa877xc, 1);
                    $(this)['attr']('style', 'border:1px red solid;');
                    free_areas['push'](_0xa877x6);
                    province_count++
                }
            }
        });
        $('#provinceCount')['text'](province_count);
        console['log'](free_areas)
    });
    $('#select_invert')['click'](function () {
        $('.ul_province li')['each'](function () {
            var _0xa877x6 = {};
            _0xa877x6['province_code'] = $(this)['attr']('id');
            _0xa877x6['province'] = $(this)['text']();
            if (typeof($(this)['attr']('style')) == 'undefined') {
                var _0xa877xc = IndexInArray(all_areas, _0xa877x6);
                if (_0xa877xc > -1) {
                    all_areas['splice'](_0xa877xc, 1);
                    $(this)['attr']('style', 'border:1px red solid;');
                    free_areas['push'](_0xa877x6);
                    province_count++
                }
            } else {
                var _0xa877xd = IndexInArray(free_areas, _0xa877x6);
                if (_0xa877xd > -1) {
                    $(this)['removeAttr']('style');
                    free_areas['splice'](_0xa877xd, 1);
                    province_count--;
                    all_areas['push'](_0xa877x6)
                }
            }
        });
        $('#provinceCount')['text'](province_count);
        console['log'](free_areas)
    });
    $('.btnSave')['click'](function () {
        var _0xa877xe = $('input[name=\'chargetype\']:checked')['val']();
        var _0xa877xf = $['trim']($('#name')['val']());
        if (_0xa877xf == '') {
            layer['msg']('\u8BF7\u8F93\u5165模板\u540D\u79F0', {
                icon: 5,
                time: 1200
            });
            return false
        };
        var _0xa877x10 = $('.distribution_area')['length'];
        var _0xa877x11 = 0;
        var _0xa877x12 = [];
        $('.distribution_area')['each'](function () {
            var _0xa877x13 = $(this)['find']('span.f_area_count')['text']();
            var _0xa877x14 = $['trim']($(this)['find']('input.f_area_first_num')['val']());
            var _0xa877x15 = $['trim']($(this)['find']('input.f_area_first_fee')['val']());
            var _0xa877x16 = $['trim']($(this)['find']('input.f_area_add_num')['val']());
            var _0xa877x17 = $['trim']($(this)['find']('input.f_area_add_fee')['val']());
            if (_0xa877x13 > 0 && _0xa877x14 != '' && _0xa877x15 != '' && _0xa877x16 != '' && _0xa877x17 != '') {
                var _0xa877x18 = {};
                _0xa877x18['first_number'] = _0xa877x14;
                _0xa877x18['first_fee'] = _0xa877x15;
                _0xa877x18['add_number'] = _0xa877x16;
                _0xa877x18['add_fee'] = _0xa877x17;
                var _0xa877x19 = [];
                $(this)['find']('.ul_area li')['each'](function (_0xa877x1a, _0xa877x1b) {
                    var _0xa877x6 = {};
                    _0xa877x6['province_code'] = $(_0xa877x1b)['attr']('id');
                    _0xa877x6['province'] = $(_0xa877x1b)['text']();
                    _0xa877x19['push'](_0xa877x6)
                });
                _0xa877x18['areas'] = _0xa877x19;
                _0xa877x12['push'](_0xa877x18);
                _0xa877x11++
            }
        });
        if (province_count == 0 && _0xa877x11 == 0) {
            layer['msg']('\u8BF7\u8BBE\u7F6E\u914D\u9001\u533A\u57DF', {
                icon: 5,
                time: 1200
            });
            return false
        };
        console['log'](_0xa877x12);
        if (_0xa877x10 > 0 && _0xa877x10 - _0xa877x11 > 1) {
            layer['msg']('\u8BF7\u5B8C\u5584\u4E0D\u5305\u90AE\u914D\u9001\u533A\u57DF', {
                icon: 5,
                time: 1200
            });
            return false
        };
        ajax('/tools/AddFreightTemplate', {
            name: _0xa877xf,
            chargetype: _0xa877xe,
            areas: free_areas,
            area_groups: _0xa877x12
        }, function (_0xa877x1c) {
            if (_0xa877x1c['status'] == 'y') {
                layer['msg']('操作成功', {
                    icon: 6,
                    time: 1200
                }, function () {
                    window['location']['href'] = '/tools/FreightTemplate'
                })
            } else {
                layer['msg'](_0xa877x1c['info'], {
                    icon: 5,
                    time: 2000
                })
            }
        })
    })
});

function AddAreaGroup(_0xa877xb) {
    var _0xa877x1e = $('input[name=\'chargetype\']:checked')['val']();
    var _0xa877x8 = _0xa877x1e == 2 ? '件' : '重';
    var _0xa877x9 = _0xa877x1e == 2 ? '件' : 'g';
    if (area_group_count > 0) {
        var _0xa877x13 = $('span.f_area_count:last')['text']();
        var _0xa877x14 = $['trim']($('input.f_area_first_num:last')['val']());
        var _0xa877x15 = $['trim']($('input.f_area_first_fee:last')['val']());
        var _0xa877x16 = $['trim']($('input.f_area_add_num:last')['val']());
        var _0xa877x17 = $['trim']($('input.f_area_add_fee:last')['val']());
        if (_0xa877x13 == 0) {
            layer['msg']('\u8BF7\u6DFB\u52A0\u533A\u57DF', {
                icon: 5,
                time: 1200
            });
            return false
        };
        if (_0xa877x14 == '' || Number(_0xa877x14) < 0) {
            layer['msg']('\u8BF7\u8F93\u5165\u6709\u6548首' + _0xa877x8, {
                icon: 5,
                time: 1200
            });
            return false
        };
        if (_0xa877x15 == '' || Number(_0xa877x15) < 0) {
            layer['msg']('\u8BF7\u8F93\u5165\u6709\u6548首\u8D39', {
                icon: 5,
                time: 1200
            });
            return false
        };
        if (_0xa877x16 == '' || Number(_0xa877x16) < 0) {
            layer['msg']('\u8BF7\u8F93\u5165\u6709\u6548续' + _0xa877x8, {
                icon: 5,
                time: 1200
            });
            return false
        };
        if (_0xa877x17 == '' || Number(_0xa877x17) < 0) {
            layer['msg']('\u8BF7\u8F93\u5165\u6709\u6548续\u8D39', {
                icon: 5,
                time: 1200
            });
            return false
        }
    };
    var _0xa877x1f = $('#add_area_group_div');
    var _0xa877x20 = '<div class="distribution_area" id="' + area_group_count_index + '_distribution_area">' + '<span style="display:none;" class="f_area_count" id="' + area_group_count_index + '_area_count">0</span>' + '<img src="/Content/img/icon_close_2.png" class="del delete" onclick="DelAreaGroup(' + area_group_count_index + ')" />' + '<div class="form-group" style="clear:both;">' + '<label class="col-sm-2" style="width:110px;margin-top:10px;">\u914D\u9001\u533A\u57DF&nbsp;&nbsp;：</label>' + '<div class="col-sm-8">' + '<ul class="ul_area" id="' + area_group_count_index + '_ul_area"></ul>' + '<select class="area_select" id="' + area_group_count_index + '_select_area" ><option value="">-请选择\u533A\u57DF-</option><option value="110000">\u5317\u4EAC</option><option value="120000">\u5929\u6D25</option><option value="130000">\u6CB3\u5317</option><option value="140000">\u5C71\u897F</option><option value="150000">\u5185\u8499\u53E4</option><option value="210000">\u8FBD\u5B81</option><option value="220000">\u5409\u6797</option><option value="230000">\u9ED1\u9F99\u6C5F</option><option value="310000">\u4E0A\u6D77</option><option value="320000">\u6C5F\u82CF</option><option value="330000">\u6D59\u6C5F</option><option value="340000">\u5B89\u5FBD</option><option value="350000">\u798F\u5EFA</option><option value="360000">\u6C5F\u897F</option><option value="370000">\u5C71\u4E1C</option><option value="410000">\u6CB3\u5357</option><option value="420000">\u6E56\u5317</option><option value="430000">\u6E56\u5357</option><option value="440000">\u5E7F\u4E1C</option><option value="450000">\u5E7F\u897F</option><option value="460000">\u6D77\u5357</option><option value="500000">重\u5E86</option><option value="510000">\u56DB\u5DDD</option><option value="520000">\u8D35\u5DDE</option><option value="530000">\u4E91\u5357</option><option value="540000">\u897F\u85CF</option><option value="610000">\u9655\u897F</option><option value="620000">\u7518\u8083</option><option value="630000">\u9752\u6D77</option><option value="640000">\u5B81\u590F</option><option value="650000">\u65B0\u7586</option><option value="710000">\u53F0\u6E7E</option><option value="810000">\u9999\u6E2F</option><option value="820000">\u6FB3\u95E8</option></select>' + '<button class="btn btn-primary add_area" id="' + area_group_count_index + '_add_area" type="button" onclick="AddArea(' + area_group_count_index + ')"><i class="fa fa-plus"></i> \u589E\u52A0</button>' + '</div>' + '</div>' + '<div class="form-group">' + '<label class="col-sm-2" style="width:110px;">\u914D\u9001\u8FD0\u8D39&nbsp;&nbsp;：</label>' + '<div class="col-sm-2 i_float_left">' + '<span  class="fee_tip01" style="padding-left:8px;">首' + _0xa877x8 + '：</span><input type="number"  id="' + area_group_count_index + '_first_number" class="form-control f_area_first_num" /> <span class="fee_tip02">&nbsp;' + _0xa877x9 + '</span>' + '</div>' + '<div class="col-sm-2 i_float_left">' + '<span>首\u8D39：</span><input type="text" id="' + area_group_count_index + '_first_fee" class="form-control f_area_first_fee" /> <span >&nbsp;\u5143</span>' + '</div>' + '<label class="col-sm-1 control-label">&nbsp;</label>' + '<div class="col-sm-2 i_float_left">' + '<span class="fee_tip11">续' + _0xa877x8 + '：</span><input type="number" id="' + area_group_count_index + '_add_number" class="form-control f_area_add_num" /> <span class="fee_tip02">&nbsp;' + _0xa877x9 + '</span>' + '</div>' + '<div class="col-sm-2 i_float_left">' + '<span>续\u8D39：</span><input type="text" id="' + area_group_count_index + '_add_fee" class="form-control f_area_add_fee" /> <span>&nbsp;\u5143</span>' + '</div>' + '</div>' + '</div>';
    _0xa877x1f['before'](_0xa877x20);
    area_group_count_index++;
    area_group_count++
}
function DelAreaGroup(_0xa877x1a) {
    $('#' + _0xa877x1a + '_distribution_area')['find']('.ul_area li')['each'](function () {
        var _0xa877x6 = {};
        _0xa877x6['province_code'] = $(area)['attr']('id');
        _0xa877x6['province'] = $(area)['text']();
        all_areas['push'](_0xa877x6)
    });
    $('#' + _0xa877x1a + '_distribution_area')['remove']();
    area_group_count--
}
function AddArea(_0xa877x1a) {
    var _0xa877x23 = $('#' + _0xa877x1a + '_select_area')['val']();
    var _0xa877x24 = $('#' + _0xa877x1a + '_select_area')['find']('option:selected')['text']();
    if (_0xa877x23 == '') {
        layer['msg']('请选择\u533A\u57DF', {
            icon: 5,
            time: 1200
        });
        return false
    };
    var _0xa877x6 = {};
    _0xa877x6['province_code'] = _0xa877x23;
    _0xa877x6['province'] = _0xa877x24;
    var _0xa877xc = IndexInArray(all_areas, _0xa877x6);
    if (_0xa877xc > -1) {
        all_areas['splice'](_0xa877xc, 1)
    } else {
        layer['msg']('\u8BE5\u533A\u57DF\u5DF2\u8BBE\u7F6E', {
            icon: 5,
            time: 1200
        });
        $('#' + _0xa877x1a + '_select_area')['val']('');
        return false
    };
    var _0xa877x25 = '<li id="' + _0xa877x23 + '">' + _0xa877x24 + '<img src="/Content/img/icon_close_alt.png" class="delete" onclick="DelArea(' + _0xa877x1a + ',this)" /></li>';
    $('#' + _0xa877x1a + '_ul_area')['append'](_0xa877x25);
    $('#' + _0xa877x1a + '_select_area')['val']('');
    var _0xa877x13 = parseInt($('#' + _0xa877x1a + '_area_count')['text']()) + 1;
    $('#' + _0xa877x1a + '_area_count')['text'](_0xa877x13);
    var _0xa877x6 = {};
    _0xa877x6['province_code'] = _0xa877x23;
    _0xa877x6['province'] = _0xa877x24
}
function DelArea(_0xa877x1a, _0xa877xb) {
    var _0xa877x27 = $(_0xa877xb)['parent']();
    var _0xa877x6 = {};
    _0xa877x6['province_code'] = _0xa877x27['val']();
    _0xa877x27['children']('img')['remove']();
    _0xa877x6['province'] = _0xa877x27['text']();
    all_areas['push'](_0xa877x6);
    _0xa877x27['remove']();
    var _0xa877x13 = parseInt($('#' + _0xa877x1a + '_area_count')['text']()) - 1;
    $('#' + _0xa877x1a + '_area_count')['text'](_0xa877x13)
}
function IndexInArray(_0xa877x29, _0xa877x2a) {
    var _0xa877xd = -1;
    for (var _0xa877x2b = 0; _0xa877x2b < _0xa877x29['length']; _0xa877x2b++) {
        if (_0xa877x29[_0xa877x2b]['province_code'] == _0xa877x2a['province_code']) {
            _0xa877xd = _0xa877x2b
        }
    };
    return _0xa877xd
}